package com.hz.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.hz.dbconn.DBConnect;
import com.hz.model.Reply;

public class ReplyControl {

	//add reply
	public static boolean addReply(Reply reply){
		DBConnect dbc = new DBConnect();
		String sql = "insert into reply (targetId,replyTime,content,uid) value(?,?,?,?)";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1,reply.getTargetId());
			dbc.setString(2, reply.getReplyTime());
			dbc.setString(3, reply.getContent());
			dbc.setInt(4, reply.getUid());
			dbc.executeUpdate();
			return true;
		}catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	//delete reply
	public static boolean deleteReply(int id) {
		DBConnect dbc = new DBConnect();
		String sql = "delete from reply where id=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	//update reply
	public static boolean updateReply(Reply reply,int id){
		DBConnect dbc = new DBConnect();
		String sql = "update reply set content=?,replyTime=? where id=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setString(1, reply.getContent());
			dbc.setString(2, reply.getReplyTime());
			dbc.setInt(3, id);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	//get reply by reply id
	public static Reply getReplyById(int id){
		DBConnect dbc = new DBConnect();
		String sql = "select * from reply where id='"+id+"'";
		dbc.prepareStatement(sql);
		try{
			Reply reply = new Reply();
			ResultSet rs = dbc.executeQuery();
			if(rs.next()){
				reply.setId(id);
				reply.setReplyTime(rs.getString("replyTime"));
				reply.setTargetId(rs.getInt("targetId"));
				reply.setContent(rs.getString("content"));
				reply.setUid(rs.getInt("uid"));
			return reply;
			}
		}catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally{
			dbc.close();
		}
		return null;
	}
	//collection reply by target id
	public static  Collection<Reply> getReplyByTargetId(int targetId) {
		DBConnect dbc = new DBConnect();
		Collection<Reply> replylist = new ArrayList<Reply>();
		String sql = "select * from reply where targetId='"+targetId+"'";
		Reply reply = null;
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				reply = new Reply();
				reply.setId(rs.getInt("id"));
				reply.setContent(rs.getString("content"));
				reply.setReplyTime(rs.getString("replyTime"));
				reply.setUid(rs.getInt("uid"));
				replylist.add(reply);
				reply = null;
			}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.print("Collection mistake!");
			return null;
		}finally{
			dbc.close();
		}
		return replylist;
	}
	//collection reply
	public static Collection<Reply> getAllReply() {
		DBConnect dbc = new DBConnect();
		String sql = "select id,targetId,content,replyTime,uid from reply";
		Collection<Reply> replylist = new ArrayList<Reply>();
		ResultSet rs = null;
		dbc.prepareStatement(sql);
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				Reply reply = new Reply();
				reply.setId(rs.getInt("id"));
				reply.setTargetId(rs.getInt("targetId"));
				reply.setContent(rs.getString("content"));
				reply.setReplyTime(rs.getString("replyTime"));
				reply.setUid(rs.getInt("uid"));
				replylist.add(reply);
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Collection mistake!");
			return null;
		}finally{
			dbc.close();
		}
		return replylist;
	}
	//test method
	//public static void main(String[] args){
		//Reply reply = new Reply();
		//reply.setContent("content");reply.setTargetId(1);reply.setReplyTime("2012-4-10");
		//ReplyControl.addReply(reply);
		//reply = ReplyControl.getReplyById(3);
		//System.out.print(reply.getReplyTime()+reply.getContent()+reply.getTargetId());
		//ReplyControl.deleteReply(2);
		//reply.setContent("update");
		//ReplyControl.updateReply(reply, 1);
		
	//}
}